/***
This do-file creates a table on the association between changes in consumer 
spending and workplace rent at the ZIP-level.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"
* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/`category'"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

project, uses("${root}/data/derived/Affinity ZCTA level data.dta")
use "${root}/data/derived/Affinity ZCTA level data.dta", clear

*-------------------------------------------------------------------------------
* 1. Effect of rent (place of work)
*-------------------------------------------------------------------------------
 
reg norm_spend_13_15_100 med_2br_rent_work_zcta ///
	if q_inc == 1 ///
	[w = pop_2018], vce(cluster county_fips)
	local coef_rent_work : di %4.2f _b[med_2br_rent_work_zcta] * 1000
local se_rent_work : di %4.2f _se[med_2br_rent_work_zcta] * 1000 
local n_rent_work: di %5.0fc `e(N)'

*-------------------------------------------------------------------------------
* 2. Effect of rent (place of work) with county FEs 
*-------------------------------------------------------------------------------

reg norm_spend_13_15_100  med_2br_rent_work_zcta ///
	if q_inc == 1 ///
	[w = pop_2018], vce(cluster county_fips) /// 
	absorb(county_fips)
local coef_rent_work_fes : di %4.2f _b[med_2br_rent_work_zcta] * 1000
local se_rent_work_fes : di %4.2f _se[med_2br_rent_work_zcta] * 1000 
local n_rent_work_fes: di %5.0fc `e(N)'

* Output 
clear 
set obs 5 
foreach spec in rent_work rent_work_fes {
	
	* Initialize 
	gen spec_`spec' = "" 
	
	* Fill in coefficients 
	replace spec_`spec' = "`coef_`spec''" in 1 
	replace spec_`spec' = "(" + "`se_`spec''" + ")" in 2 
	replace spec_`spec' = "`n_`spec''" in 5 
}

* Export
export excel "${root}/results/new_app_table_7.xlsx", sheet("new_app_table_7", replace) firstrow(variables) 
project, creates("${root}/results/new_app_table_7.xlsx")

* Scalars for table notes 
cap erase "${root}/results/paper numbers/`category'/Association Between Changes in Consumer Spending and Workplace Rent by ZIP Code.yaml"

local abs_coef_rent_work = abs(`coef_rent_work')

yamlout using "${root}/results/paper numbers/`category'/Association Between Changes in Consumer Spending and Workplace Rent by ZIP Code.yaml", ///
	key("coef_rent_work_apr_jan") ///
	comment("Slope (-) of low-income spending vs. workplace rent, at the ZIP level") ///
	value(`abs_coef_rent_work')


